package com.bjsxt.service.impl;

import com.bjsxt.mapper.AdminMapper;
import com.bjsxt.mapper.AdminRoleMapper;
import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;

    //查询登录
    @Override
    public Admin selectAdminSice(String aname, String apwd) {
        return adminMapper.selectAdmin(aname, apwd);
    }

    @Override
    public PageA<Admin> selectAdminAllSice(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Admin> list = adminMapper.selectAdminAll();
        PageA<Admin> pgg=new PageA<>(list,pg.getTotal());

        return pgg;
    }

    //添加管理员
    @Override
    public Integer insertAdmin(Admin admin,Integer aid, Integer rid) {
        Integer a=0;
        try {
            Date d=new Date(new java.util.Date().getTime());
            admin.setStarttime(d);
            adminMapper.insertAdmin(admin);
            adminRoleMapper.insertAdminRole(admin.getAid(),rid);
            a=1;
        } catch (Exception e) {
            throw e;
        }


        return a;
    }

    @Override
    public Integer updateAdmin(Admin admin,Integer aid,Integer rid) {
        Integer a=0;
        try {
            adminMapper.updateAdmin(admin);
            adminRoleMapper.updateAdminRole(rid,aid);
            a=1;
        } catch (Exception e) {
            throw e;
        }

        return a;
    }

    @Override
    public Integer deleteAdminSice(Integer aid) {
        return adminMapper.deleteAdmin(aid);
    }
}
